#pragma once
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<functional>
#include<ctime>
#include<unordered_map>
using task_t=std::function<void()>;
class TaskManger
{
    public:
    TaskManger()
    {
        srand(time(nullptr));
        //随便写一些编号对应的任务
        tasks.push_back([](){std::cout<<"sub process:"<<getpid()<<" 正在执行访问数据库任务"<<std::endl;});
        tasks.push_back([](){std::cout<<"sub process:"<<getpid()<<" 正在执行url解析"<<std::endl;});
        tasks.push_back([](){std::cout<<"sub process:"<<getpid()<<" 正在执行加密任务"<<std::endl;});
        tasks.push_back([](){std::cout<<"sub process:"<<getpid()<<" 正在执行数据持久化任务"<<std::endl;});
    }
    int SelectTask()
    {
        return rand()%tasks.size();
    }
    void execute(unsigned long number)
    {
        if(number>=0&&number<tasks.size())
            tasks[number]();
    }
    private:
    std::vector<task_t> tasks;
};

TaskManger TM;
void Worker()
{
    while(true)
    {
        int cmd;
        int n=read(0,&cmd,sizeof(cmd));
        if(n==sizeof(cmd))
            TM.execute(cmd);
        else if(n==0)
        {
            //另一端关闭了写端，即程序退出
            std::cout << "pid: " << getpid() << " quit..." << std::endl;
            break;
        }
    }
}